package com.util.seo;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinYinTool {

	/**
	 * 返回单个字符拼音
	 * @param chineseCode
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public static String getPinYin(char chineseCode) throws BadHanyuPinyinOutputFormatCombination {
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.restoreDefault();
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		String[] chinese = PinyinHelper.toHanyuPinyinStringArray(chineseCode, outputFormat);
		return chinese.toString();
	}

	/**
	 * 返回多音字拼音数组
	 * 
	 * @param chineseCode
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public static String[] getAllPinYin(char chineseCode) throws BadHanyuPinyinOutputFormatCombination {
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray( chineseCode, outputFormat);
		//System.out.println("***");
		return pinyinArray;
	}

	/**
	 * 按音调取多音字拼音
	 * 
	 * @param chineseCode
	 * @param yinDiao
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public static String getPinYinByYinDiao(char chineseCode, int yinDiao) throws BadHanyuPinyinOutputFormatCombination {

		String[] pinyinArray = getAllPinYin(chineseCode);
		String py = "";
		for (int i = 0; i < pinyinArray.length; i++) {
			String tempStr = pinyinArray[i];
			String tempStr2 = tempStr.substring(tempStr.length() - 1, tempStr.length());
			if (Integer.parseInt(tempStr2) == yinDiao)
				py = tempStr.substring(0, tempStr.length() - 1);
		}
		// System.out.println("py== "+py);
		return py;
	}

	/**
	 * 返回有间隔的拼音字符串 如：pin yin
	 * 
	 * @param chineseCode 中文字符串
	 * @return
	 */
	public static String getPinYin(String chineseStr) {
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		String pinyinCode = "";
		if (chineseStr == null || chineseStr.length() == 0)
			return pinyinCode;
		try {
			outputFormat.restoreDefault();
			outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
			pinyinCode = PinyinHelper.toHanyuPinyinString(chineseStr, outputFormat, " ");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pinyinCode;
	}

	/**
	 * 返回无间隔的拼音字符串 如：pinyin
	 * 
	 * @param chineseCode 中文字符串
	 * @return
	 */
	public static String getPinYinStr(String chineseStr) {
		char[] t1;
		t1 = chineseStr.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符函数
				if (java.lang.Character.toString(t1[i]).matches(
						"[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else
					t4 += java.lang.Character.toString(t1[i]);
			}
			// System.out.println(t4);
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}

	/**
	 * 返回大写的拼音字符串
	 * 
	 * @param chineseCode 中文字符串
	 * @return
	 */
	public static String getUppercasePinYin(String chineseStr) {
		char[] t1;
		t1 = chineseStr.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符函数
				if (java.lang.Character.toString(t1[i]).matches(
						"[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else
					t4 += java.lang.Character.toString(t1[i]);
			}
			// System.out.println(t4);
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}

	/**
	 * 过滤重庆(zhong qing)转为重庆
	 * @param chineseCode
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public static String getQuanPin(String chineseCode) throws BadHanyuPinyinOutputFormatCombination {
		if(chineseCode == null || chineseCode.length()==0)
			return "";
		String chinesePinyin = PinYinTool.getPinYin(chineseCode);
		if (chineseCode.length() >= 2)
			if (chineseCode.substring(0, 2).equals("重庆")) {
				char[] arr = chineseCode.toCharArray();
				chineseCode = PinYinTool.getPinYinByYinDiao(arr[0], 2);
				chinesePinyin = chineseCode + chinesePinyin.substring(chinesePinyin.indexOf(" "), chinesePinyin.length());
			}
		return chinesePinyin;
	}

	/**
	 * 各种样式的拼音
	 * 
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public static void test() throws BadHanyuPinyinOutputFormatCombination {
		// deal with Chinese sentence
		{
			HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
			// defautlt output format
			System.out.println("defautlt output format");
			System.out.println(PinyinHelper.toHanyuPinyinString("我的中国心",
					outputFormat, ""));
			System.out.println("***");
			// with seperater
			System.out.println("with seperater");
			System.out.println(PinyinHelper.toHanyuPinyinString("我的中国心",
					outputFormat, " "));
			System.out.println("***");
			// output uppercase
			System.out.println("output uppercase");
			outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
			System.out.println(PinyinHelper.toHanyuPinyinString("我的中国心",
					outputFormat, " "));
			System.out.println("***");
			// without tone numbers or tone marks
			System.out.println("without tone numbers or tone marks");
			outputFormat.restoreDefault();
			outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
			System.out.println(PinyinHelper.toHanyuPinyinString("s3我的中国心",
					outputFormat, " "));
			System.out.println("***");
			// use u: as an alternative of 'ü'
			System.out.println("use u: as an alternative of 'ü'");
			outputFormat.restoreDefault();
			System.out.println(PinyinHelper.toHanyuPinyinString("越女剑",
					outputFormat, " "));
			System.out.println("***");
			// use v as an alternative of 'ü'
			System.out.println("use v as an alternative of 'ü'");
			outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
			System.out.println(PinyinHelper.toHanyuPinyinString("越女剑",
					outputFormat, " "));
			System.out.println("***");
			// directly output 'ü' in Unicode form
			System.out.println("directly output 'ü' in Unicode form");
			outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
			System.out.println(PinyinHelper.toHanyuPinyinString("越女剑",
					outputFormat, " "));
			System.out.println("***");
			// deal with combination of Chinese and non-Chinese
			System.out
					.println("deal with combination of Chinese and non-Chinese");
			outputFormat.restoreDefault();
			System.out.println(PinyinHelper.toHanyuPinyinString("5%道琼斯指数上扬",
					outputFormat, " "));
			System.out.println("***");
		}
		// deal with Chinese character
		{
			HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
			{
				// single pronounciation
				String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(
						'我', outputFormat);
				System.out.println("***");
				for (int i = 0; i < pinyinArray.length; i++) {
					StringBuffer stringbuf = new StringBuffer();
					stringbuf.append("pinyin ");
					stringbuf.append(i);
					stringbuf.append(": ");
					stringbuf.append(pinyinArray[i]);
					System.out.println(stringbuf.toString());
				}
				System.out.println("***");
			}
			{
				// multiple pronounciations
				String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray('中', outputFormat);
				System.out.println("***");
				for (int i = 0; i < pinyinArray.length; i++) {
					StringBuffer stringbuf = new StringBuffer();
					stringbuf.append("pinyin ");
					stringbuf.append(i);
					stringbuf.append(": ");
					stringbuf.append(pinyinArray[i]);
					System.out.println(stringbuf.toString());
				}
				System.out.println("***");
			}
		}
	}

	public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
		// test();
		String str = "腌";
		getPinYin(str);
		System.out.println(getAllPinYin('腌').clone()[1]);
	}

}
